import java.util.Comparator;

public class Student implements Comparable<Student> {
    public int age;
    public String name;
    public Student(int age, String name) {
        this.age = age;
        this.name = name;
    }

    //方法一 ： 重写父类 equals 方法

    @Override
    public boolean equals(Object o) {//Student的equals方法
        if (this == o)
            return true;//自己和自己比

        if (o == null || !(o instanceof Student))
            return false;//o为空 或 o不是Student或其子类

        Student s = (Student) o;//向下转型
        return age == s.age && s.name.equals(this.name);//String的equals方法
    }

    //方法二 ： 实现 Compareable 接口，重写 compareTo 方法

    @Override
    public int compareTo(Student o) {
        return o.age - this.age;
    }
}
class StudentComparator implements Comparator<Student> {

    //方法三 ： 自定义比较器类，实现 Comparactor 接口，重写 compare 方法

    @Override
    public int compare(Student o1, Student o2) {
        if(o1 == o2)
            return 0;
        if(o1 == null )
            return -1;
        if(o2 == null)
            return 1;
        return o1.age - o2.age;
    }
}
class Test {
    public static void main(String[] args) {
        StudentComparator studentComparator = new StudentComparator();
        Student student1 = new Student(10,"光光");
        Student student2 = new Student(12,"对立");
        System.out.println(studentComparator.compare(student1, student2));
    }
}
